<%- include('css') %>
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>安全设置</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-inline">
                <input type="password" name="oldpassword" id="oldpassword" required lay-verify="required" placeholder="请输入原密码"
                    autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">用于身份确认</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入新密码" autocomplete="off"
                    class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux" id=''>6-16字母或数字</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="repassword" required lay-verify="required" placeholder="请再次确认密码"
                    autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">与新密码保持一致</div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">保存</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>

<script>
    //密码 监听 验证
    var psd_yanzheng = false;
    var psd_yanzheng1 = false;
    var psd_yanzheng2 = false;
    var psd_yanzheng3 = false;
    $(document).ready(function () {
        $('input').bind('input propertychange', function () {
            var myreg = /^[0-9a-zA-Z]{6,16}$/;
            if ($(this).val()) {
                var that = this;
                if ($(this).attr("name") == 'oldpassword') {
                    //原密码
                    var oldpassword = $(this).val();
                    if (oldpassword.length < 6) {
                        return false;
                    }else{
                        psd_yanzheng1 = true;
                    }
                } else if ($(this).attr("name") == 'password') {
                    //新密码
                    if (!myreg.test($(this).val())) {
                        $(this).parent().next().css("color", "#FF0000");
                        $(this).parent().next().text('密码不符合规范');
                        psd_yanzheng2 = false;
                    } else {
                        $(this).parent().next().text('验证通过');
                        psd_yanzheng2 = true;
                    }
                } else {
                    //重复密码
                    if ($("input[name='password']").val() != $(this).val()) {
                        $(this).parent().next().css("color", "#FF0000");
                        $(this).parent().next().text('与新密码不一致');
                        psd_yanzheng3 = false;
                    } else {
                        $(this).parent().next().text('验证通过');
                        psd_yanzheng3 = true;
                    }
                }
            }
            if (psd_yanzheng1 == true && psd_yanzheng2 == true && psd_yanzheng3 == true) {
                psd_yanzheng = true;
            }
        })
    });


    var vm = new Vue({
        el: '#app',
        data: function () {

            return {
                isButtonDisabled: false
            }

        },
        mounted: function () {

            var self = this;
            layui.use(['form', 'layer'], function () {
                var form = layui.form;
                var layer = layui.layer;
                //监听提交
                form.on('submit(save)', function (data) {
                    if (psd_yanzheng == false) {
                        return false;
                    }
                    self.isButtonDisabled = true;
                    $.post(
                        '/admin/access/changepassword',
                        { data: data.field },
                        function (res) {
                            if (res.code === 200) {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                layer.msg('修改成功！请手动退出重新登录', { icon: 1 });
                                self.to_login();
                            } else {
                                layer.msg('修改失败:'+res.error+' -- '+res.code,{icon:2});
                            }
                            self.isButtonDisabled = false;
                        }
                    )
                    return false;
                });
            });
        },
        methods: {
            doclose: function () {
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            }
        }
    })



</script>
